package activities.project3.group3.s12cs342;

import java.io.Serializable;
import java.util.ArrayList;

import DataStructures.Project3.Group3.s12cs342.QuestionContainer;
import DataStructures.Project3.Group3.s12cs342.QuizContainer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class TakeQuizActivity extends Activity {

	private QuizContainer currentQuiz;
	private ArrayList<QuestionContainer> currentQuizQuestions;
	private QuestionContainer currentQuestion;
	private Button previous, save, next;
	private TextView question;
	private RadioButton aRadio, bRadio, cRadio, dRadio, trueRadio, falseRadio;
	private int currentQuestionIndex;
	private boolean answered = false;
	private boolean wrong = false;
	private EditText fillin;
	String answer = " ";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Quiz mApplication = ((Quiz) getApplicationContext());
		currentQuizQuestions = mApplication.getQuestions();
		currentQuestionIndex = 0;

	}

	@Override
	public void onResume() {
		super.onResume();

		answered = false;
		wrong = false;
		if(currentQuestionIndex < currentQuizQuestions.size() ){
			
		currentQuestion = currentQuizQuestions.get(currentQuestionIndex);
		if (currentQuestion.getType().equals("Multiple Choice")) {
			setContentView(R.layout.mult_choice);

			previous = (Button) findViewById(R.id.previousBtn);
			save = (Button) findViewById(R.id.saveBtn);
			next = (Button) findViewById(R.id.nextBtn);
			question = (TextView) findViewById(R.id.questionTextView);
			aRadio = (RadioButton) findViewById(R.id.aRadioBtn);
			bRadio = (RadioButton) findViewById(R.id.bRadioBtn);
			cRadio = (RadioButton) findViewById(R.id.cRadioBtn);
			dRadio = (RadioButton) findViewById(R.id.dRadioBtn);
			RadioGroup radio = (RadioGroup) findViewById(R.id.radioGroup1);
			question.setText(currentQuestion.getQuestion());
			String[] temp = currentQuestion.getAnswers();
			aRadio.setText(temp[0]);
			bRadio.setText(temp[1]);
			cRadio.setText(temp[2]);
			dRadio.setText(temp[3]);

			next.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					
					if (answer.equals(currentQuestion.getCorrectAnswer()) || answered == true) {
							Quiz mApplication = ((Quiz)getApplicationContext());
							if(wrong == false){
								mApplication.setCurrentScore(currentQuestion.getPointsWorth());
							Toast.makeText(getApplicationContext(), "Correct!",
									Toast.LENGTH_SHORT).show();
							}
							
				 			mApplication.setTotalScore(currentQuestion.getPointsWorth());
				 			
							currentQuestionIndex++;
							onResume();
						}
						else{
							Toast.makeText(getApplicationContext(), "The answer is " + currentQuestion.getCorrectAnswer(), Toast.LENGTH_LONG).show();
							answered = true;
							wrong = true;
						}
				}

			});

			previous.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					if (currentQuestionIndex > 0) {
						currentQuestionIndex--;
						onResume();
					}
				}

			});

			radio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

				public void onCheckedChanged(RadioGroup group, int checkedId) {
					RadioButton checkedRadioButton = (RadioButton) findViewById(checkedId);
					answer = checkedRadioButton.getText().toString();
					

				}
			});

		} else if (currentQuestion.getType().equals("True or False")) {
			setContentView(R.layout.true_false);
			previous = (Button) findViewById(R.id.previousBtn);
			save = (Button) findViewById(R.id.saveBtn);
			next = (Button) findViewById(R.id.nextBtn);
			question = (TextView) findViewById(R.id.questionTextView);
			aRadio = (RadioButton) findViewById(R.id.aRadioBtn);
			bRadio = (RadioButton) findViewById(R.id.bRadioBtn);
			cRadio = (RadioButton) findViewById(R.id.cRadioBtn);
			dRadio = (RadioButton) findViewById(R.id.dRadioBtn);
			trueRadio = (RadioButton) findViewById(R.id.trueRadioBtn);
			falseRadio = (RadioButton) findViewById(R.id.falseRadioBtn);
			RadioGroup radio = (RadioGroup) findViewById(R.id.radioGroup1);
			
			question.setText(currentQuestion.getQuestion());

			next.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					if (answer.equals(currentQuestion.isTFAnswer()) || answered == true) {
						Quiz mApplication = ((Quiz)getApplicationContext());
						if(wrong == false){
							mApplication.setCurrentScore(currentQuestion.getPointsWorth());
						Toast.makeText(getApplicationContext(), "Correct!",
								Toast.LENGTH_SHORT).show();
						}
						mApplication.setTotalScore(currentQuestion.getPointsWorth());
						currentQuestionIndex++;
						onResume();
					}
					else{
						Toast.makeText(getApplicationContext(), "The answer is " + currentQuestion.isTFAnswer(), Toast.LENGTH_LONG).show();
						answered = true;
						wrong = true;
					}

				}

			});

			previous.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					if (currentQuestionIndex > 0) {
						currentQuestionIndex--;
						onResume();
					}
				}

			});
			
			radio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

				public void onCheckedChanged(RadioGroup group, int checkedId) {
					RadioButton checkedRadioButton = (RadioButton) findViewById(checkedId);
					answer = checkedRadioButton.getText().toString();
				

				}
			});
			
		} else if (currentQuestion.getType().equals("Short Answer")) {
			setContentView(R.layout.fill_blank);
			previous = (Button) findViewById(R.id.previousBtn);
			save = (Button) findViewById(R.id.saveBtn);
			next = (Button) findViewById(R.id.nextBtn);
			question = (TextView) findViewById(R.id.questionTextView);
			fillin = (EditText)findViewById(R.id.editText1);
			

			question.setText(currentQuestion.getQuestion());

			next.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					String t = fillin.getText().toString();
					t.trim();
					if (t.equals(currentQuestion.getCorrectAnswer()) || answered == true) {
						Quiz mApplication = ((Quiz)getApplicationContext());
						
						if(wrong == false){
							mApplication.setCurrentScore(currentQuestion.getPointsWorth());
						Toast.makeText(getApplicationContext(), "Correct!",
								Toast.LENGTH_SHORT).show();
						}
						mApplication.setTotalScore(currentQuestion.getPointsWorth());
						currentQuestionIndex++;
						onResume();
					}
					else{
						Toast.makeText(getApplicationContext(), "The correct answer is "  + currentQuestion.getCorrectAnswer(), Toast.LENGTH_LONG).show();
						wrong = true;
						answered = true;
					}
					
				}

			});

			previous.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					if (currentQuestionIndex > 0) {
						currentQuestionIndex--;
						onResume();
					}

				}

			});
		}}
		else{
			Intent i = new Intent(TakeQuizActivity.this, ShowScoresActivity.class);
			
			startActivity(i);
		}

	}

}

